package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.hosp.repository.HospitalRepository;
import com.atguigu.yygh.hosp.repository.ScheduleRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository  hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> paramMap) {
        // 先把参数的map集合转换成对象
        String mapString = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);

        //先判断是否存在相同的数据
        String hoscode = hospital.getHoscode();
            Hospital hospitalExist  =hospitalRepository.getHospitalByHoscode(hoscode);



        //如果存在，进行修改
   if(hospitalExist !=null){

       hospital.setStatus(hospitalExist.getStatus());
       hospital.setCreateTime(hospitalExist.getCreateTime());
       hospital.setUpdateTime(new Date());
       hospital.setIsDeleted(0);
       hospitalRepository.save(hospital);
   }else{ // 如果bu存在，进行添加

    hospital.setStatus(0);
    hospital.setCreateTime(new Date());
    hospital.setUpdateTime(new Date());
    hospital.setIsDeleted(0);
    hospitalRepository.save(hospital);

   }





    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospitalByHoscode = hospitalRepository.getHospitalByHoscode(hoscode);

          if(hospitalByHoscode==null){

              throw new YyghException("失败",201);
          }


        return hospitalByHoscode;
    }

    @Override
    public Page<Hospital> selectHospPage(Long page, Long limit, HospitalQueryVo hospitalSetQueryVo) {

        //创建Pageable对象 设置当前页和每页记录数
        Pageable pageable= PageRequest.of((int)(page-1),limit.intValue());

       Hospital hospital=new Hospital();
        BeanUtils.copyProperties(hospitalSetQueryVo,hospital);
        hospital.setIsDeleted(0);

        //创建 Example 对象
        ExampleMatcher matcher=ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        Example<Hospital> example= Example.of(hospital,matcher);

        Page<Hospital> all = hospitalRepository.findAll(example, pageable);


        all.getContent().stream().forEach(item ->{

            this.setHospitalHosType(item);

        });





        return all;
    }

    @Override
    public void updateStatus(String id, Integer status) {

            Hospital hospital   =hospitalRepository.findById(id).get();

            if(hospital==null){
                throw new YyghException("id错误",201);

            }else{

                hospital.setStatus(status);
                hospital.setUpdateTime(new Date());
                hospitalRepository.save(hospital);
            }





    }

    @Override
    public Map<String,Object> getHospById(String id) {
        Map<String,Object> map=new HashMap<>();
        Hospital hospital = hospitalRepository.findById(id).get();
        Hospital hospitalAll= setHospitalHosType(hospital);
// 医院的基本信息
   map.put("hospital",hospitalAll);
   //医院预约信息
   map.put("bookingRule",hospitalAll.getBookingRule());

   hospitalAll.setBookingRule(null);

   return map;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospitalByHoscode = hospitalRepository.getHospitalByHoscode(hoscode);
    if(hospitalByHoscode==null){

        throw  new YyghException("错误",201);
    }
        return hospitalByHoscode.getHosname();
    }

    @Override
    public List<Schedule> getDetailScheduls(String hoscode, String depcode, Date workDate) {


        return null;
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {




        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    @Override
    public Map<String, Object> item(String hoscode) {
  Map<String,Object> resule=new HashMap<>();

        Hospital hospital = setHospitalHosType(getByHoscode(hoscode));

        resule.put("hospital",hospital);
        resule.put("bookingRule",hospital.getBookingRule());

         hospital.setBookingRule(null);


        return resule;
    }

    private Hospital setHospitalHosType(Hospital hospital) {
   //更具dictCOde 和value获取医院等级名称
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());

 // 查询 省 市 地区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("hostypeString",hostypeString);
        hospital.getParam().put("fullAddress",provinceString+cityString+districtString);



        return  hospital;



    }


}
